package com.ioidea.ble.proto

import com.ioidea.base.util.Symbol
import com.ioidea.ble.bean.JkqMqttInfo

/**
 * 因为没有plc设备
 * 测试代码  测试路灯集控器协议
 */
class ProtoGetConfig(override var requestCommand: String="GET_CFG",
                     override var responseCommand: String="GET_CFG"
) : ProtoBase() {

    companion object{
        val READ_DEV_HEAD = "@GET_CFG&"
        val READ_DEV_TAIL = "$#@"
    }



    fun getConfigCommand(sn: String): String {
        return READ_DEV_HEAD + sn + READ_DEV_TAIL
    }

    fun parseConfig(sn: String, data: String) : JkqMqttInfo? {
        if (sn != EMPTY_SN) {
            val head = READ_DEV_HEAD + sn
            if (data.startsWith(head)) {//成功的数据
               val dataArray= data.split(Symbol.YU)
                return getJkqMqttInfo(dataArray)

            }
        }
        return null
    }

    private fun getJkqMqttInfo(dataArray: List<String>): JkqMqttInfo {
        val jkqMqttInfo= JkqMqttInfo()
        jkqMqttInfo.sn=dataArray[1]
        jkqMqttInfo.ip=dataArray[2]
        jkqMqttInfo.port=dataArray[3]
        jkqMqttInfo.mqttAccount=dataArray[4]
        jkqMqttInfo.mqttPwd=dataArray[5]
        jkqMqttInfo.pub=dataArray[6]
        jkqMqttInfo.sub=dataArray[7]
        jkqMqttInfo.lastWill=dataArray[8]
        return jkqMqttInfo

    }


}


